

package cn.shengchao.examstar.distributor.gateway.controller;

import cn.shengchao.examstar.distribute.infrastrure.po.DistributeNodeInfo;
import cn.shengchao.examstar.distribute.infrastrure.service.DistributeNodeInfoService;
import cn.shengchao.examstar.distributor.gateway.service.DistributorService;
import cn.shengchao.examstar.distributor.gateway.service.message.DistributorProfileUpdateRequest;
import cn.shengchao.examstar.rpc.distribute.enums.DistributeSaleRoleEnum;
import cn.shengchao.util.helper.LoginHelper;
import com.google.common.collect.Maps;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import top.continew.starter.web.model.R;

@RestController
@Slf4j
@CrossOrigin("${project.cross-origin}")
@RequestMapping("/api/${project.version}/distributor")
public class DistributorController {

    @Autowired
    private DistributeNodeInfoService distributeNodeInfoService;

    @Autowired
    private DistributorService distributorService;

    /**
     * 检查是否是加盟商
     */
    @GetMapping("checkDistributor")
    public R checkDistributor() {
        DistributeNodeInfo distributeNodeInfo = distributeNodeInfoService.getDistributeNodeInfo(LoginHelper.getUserId(), DistributeSaleRoleEnum.ROLE_DSPD);
        return R.ok(Maps.immutableEntry("isDistributor", distributeNodeInfo != null));
    }

    // 修改加盟商资料接口
    @PutMapping("/updateProfile")
    public void updateProfile(@RequestBody DistributorProfileUpdateRequest request) {
        Long userId = LoginHelper.getLoginUser().getId();
        distributorService.updateProfile(userId, request.getHeadImageUrl(), request.getFullName(), DistributeSaleRoleEnum.getByCode(request.getCode()));
    }
}
